rm(list=ls())
library(tidyverse)

#Reads CSV Files from BOLSA FAMILIA

bf_files <- list.files(here::here("data","raw","bolsa_familia"), pattern = ".csv", full.names = T)

list_bolsa_fam <- map(bf_files,read_csv)

panel_bolsa_fam <- bind_rows(list_bolsa_fam)

#Process files 
panel_bolsa_fam <- panel_bolsa_fam %>%
  separate(col = anomes,sep = "(?<=^\\d{4})",into = c("year","month")) %>% 
  mutate(year = as.numeric(year),
         mun_code = as.character(ibge)) %>% 
  select(-ibge) %>% 
  ungroup()

#Creates summary dataframe
summary_bolsa_fam <- panel_bolsa_fam %>%
  group_by(mun_code,year) %>% 
  summarise(monthly_avg_value = mean(valor_repassado_bolsa_familia, na.rm = T),
            total_annual_value = sum(valor_repassado_bolsa_familia, na.rm = T),
            monthly_avg_households = mean(qtd_familias_beneficiarias_bolsa_familia, na.rm =T)) %>% 
  ungroup()

# Create BF constant prices and per capita

gdp_deflator <- read_rds(here::here("data","processed","gdp_deflator.rds"))

mun_pop <- read_rds(here::here("data","processed","citycharacteristics","pop_mun.rds")) %>% 
  select(-mun_name)

bf <- summary_bolsa_fam %>% 
  left_join(gdp_deflator, by = "year") %>% 
  left_join(mun_pop, by = c("mun_code","year")) %>% 
  filter(year <= 2016)

bf_outcomes <- bf %>% 
   mutate(total_cp_bf = total_annual_value/gdp_deflator,
          total_annual_value_pc_bf = total_cp_bf/mun_pop,
          monthly_avg_households_pc_bf = monthly_avg_households/mun_pop) %>% 
  select(mun_code,year,total_cp_bf,total_annual_value_pc_bf,monthly_avg_households_pc_bf,monthly_avg_households)
 
#Saves summary table

write_rds(bf_outcomes,path = here::here("data","processed","citycharacteristics","bolsa_fam.rds"))


